<?php
/**
 * Created by Sms.php.
 * User: wy
 * Date: 2019/4/27
 * Time: 18:41
 */

namespace app\index\controller;


use my\Security;
use my\Tools;
use think\Cache;
use think\Controller;

class Sms extends Controller
{
    /*
     * 合肥注册用户发短信
     */
    public function hf()
    {
        if(date('Y-m-d H:i:s')>='2019-04-01 00:00:00' && date('Y-m-d H:i:s')<'2019-04-02 00:00:00'){
            $user_phone = Cache::get('user_phone');
            if(empty($user_phone)){
                $user_phone = db('user')->where('register_city', '133')->getField('uid,user_phone');
                foreach ($user_phone as $k => $v) {
                    $v = Security::decrypt($v);
                    if ($v) {
                        $user_phone[$k] = $v;
                    }
                }
                Cache::set('user_phone',array_unique($user_phone),86400);
            }
            //200一次打包
            $new_arr = [];
            $count = count($user_phone);
            for($i=0;$i<$count;$i+=200){
                $new_arr[] = implode(",",array_slice($user_phone,$i,200));
            }
            $new_count = count($new_arr);
            for($i=0;$i<$new_count;$i++){
                $this->sendUser($new_arr[$i]);
                usleep(200000);     //等待2秒
            }
        }else{
            return '未在指定时间！';
        }
    }

    //发送短信
    protected function sendUser($str){
        //$user_phone = $str;
        //$res = Tools::sendMes($user_phone,'用户',36);
        //print_r($res);
    }

    public function export(){
        $list = db('user')->where('register_city', '133')->getField('uid,user_phone');
        header ( "Content-type:application/vnd.ms-excel" );
        header ( "Content-Disposition:attachment;filename=tp.xls" );
        echo iconv ( 'UTF-8', 'GBK', "手机号\t\n");
        foreach ( $list as $k => $v ) {
            echo Security::decrypt($v). "\t";
            echo "\n";
        }
    }

}